package com.ghh.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ghh.constant.MessageConstant;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.entity.Result;
import com.ghh.pojo.CheckItem;
import com.ghh.service.CheckitemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckitemController {

    @Reference
    private CheckitemService checkitemService;


    //添加方法检查项
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("/addCheckitem")
    public Result addCheckitem(@RequestBody CheckItem checkItem){
        try {
            checkitemService.addCheckitem(checkItem);
            //添加成功
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    //分页查询检查项
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkitemService.pageQuery(queryPageBean);
    }

    //删除检查项
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deleteCheckitem")
    public Result deleteCheckitem(int id){
        try {
            checkitemService.deleteCheckitem(id);
            //删除成功
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    //修改检查项
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/updateCheckitem")
    public Result updateCheckitem(@RequestBody CheckItem checkItem){
        try {
            checkitemService.updateCheckitem(checkItem);
            //修改成功
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    //根据ID查找检查项
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findById")
    public Result findById(int id){
        CheckItem checkItem = null ;
        try {
            checkItem = checkitemService.findById(id);
            //查询成功
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //查询所有检查项
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findAllCheckitem")
    public Result findAllCheckitem(){

        try {
            List<CheckItem> list = checkitemService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
